package ran.mail;


import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-11-03
 * Time: 17:50
 */
@Component
public class MailService {


    private static final Logger log = LoggerFactory.getLogger(MailService.class);
    @Autowired
    JavaMailSender javaMailSender;

    public boolean sendMail(String toMail, String code) {

        try {
            // 创建一个邮件消息
            MimeMessage message = javaMailSender.createMimeMessage();

            // 创建 MimeMessageHelper
            MimeMessageHelper helper = new MimeMessageHelper(message, false);

            // 发件人邮箱和名称
            helper.setFrom("jh_ran@126.com", "oj答题系统");
            // 收件人邮箱
            helper.setTo(toMail);
            // 邮件标题
            helper.setSubject("登录验证码");
            // 邮件正文，第二个参数表示是否是HTML正文
            //" <strong> 这是你的验证码: "+code+"</strong>\n" +
            //                "\n<strong> 请妥善保管! </strong>" +"<!DOCTYPE html>\n" +
            helper.setText(
                    "<html lang=\"zh-Hans\">\n" +
                            "<head>\n" +
                            "   <meta charset=\"UTF-8\">\n" +
                            "   <title>邮箱验证码</title>\n" +
                            "   <style>\n" +
                            "       body {\n" +
                            "           font-family: Arial, sans-serif;\n" +
                            "           line-height: 1.6;\n" +
                            "           background-color: #f9f9f9;\n" +
                            "           margin: 0;\n" +
                            "           padding: 0;\n" +
                            "       }\n" +
                            "       .container {\n" +
                            "           width: 600px;\n" +
                            "           margin: 50px auto;\n" +
                            "           background: #fff;\n" +
                            "           border-radius: 8px;\n" +
                            "           box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);\n" +
                            "           padding: 20px;\n" +
                            "       }\n" +
                            "       .header {\n" +
                            "           text-align: center;\n" +
                            "           font-size: 24px;\n" +
                            "           color: #333;\n" +
                            "           margin-bottom: 20px;\n" +
                            "       }\n" +
                            "       .content {\n" +
                            "           font-size: 16px;\n" +
                            "           color: #555;\n" +
                            "       }\n" +
                            "       .code {\n" +
                            "           font-size: 24px;\n" +
                            "           color: #ff5722;\n" +
                            "           font-weight: bold;\n" +
                            "           text-align: center;\n" +
                            "           margin: 20px 0;\n" +
                            "       }\n" +
                            "       .footer {\n" +
                            "           font-size: 12px;\n" +
                            "           color: #999;\n" +
                            "           text-align: center;\n" +
                            "           margin-top: 30px;\n" +
                            "       }\n" +
                            "   </style>\n" +
                            "</head>\n" +
                            "<body>\n" +
                            "   <div class=\"container\">\n" +
                            "       <div class=\"header\">邮箱验证码</div>\n" +
                            "       <div class=\"content\">\n" +
                            "           尊敬的用户，您好！<br><br>\n" +
                            "           您正在进行<span style=\"color:#007bff;\">登录/注册账号</span>操作，请在验证码输入框中输入以下验证码完成操作：\n" +
                            "       </div>\n" +
                            "       <div class=\"code\">"+code+"</div>\n" +
                            "       <div class=\"content\">\n" +
                            "           注意：此验证码5分钟内有效。如非本人操作，请忽略此邮件。\n" +
                            "       </div>\n" +
                            "       <div class=\"footer\">\n" +
                            "           此邮件由系统自动发送，请勿回复。\n" +
                            "       </div>\n" +
                            "   </div>\n" +
                            "</body>\n" +
                            "</html>", true);

            // 发送
            javaMailSender.send(message);
            log.info("邮件验证码发送成功, code:{}",code);
            return true;
        }catch (Exception e) {
            log.error("邮件验证码发送失败, code:{}",code);
            return false;
        }
    }
}
